using System;
using System.Data;
using System.Web.UI;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
public partial class Calendar_New : Page
{
    private DataTable table;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBoxStart.Text = Convert.ToDateTime(Request.QueryString["start"]).ToString();
            TextBoxEnd.Text = Convert.ToDateTime(Request.QueryString["end"]).ToString();

            TextBoxName.Focus();
        }
    }
    protected void ButtonOK_Click(object sender, EventArgs e)
    {
        // Session["dcotorId"] = Request.QueryString["Id"].ToString();
        string id = Session["userid"].ToString();
        if (!String.IsNullOrEmpty(id))
            {
                CalendarEvent cev = new CalendarEvent();
                cev.eventstart = Convert.ToDateTime(TextBoxStart.Text);
                cev.eventend = Convert.ToDateTime(TextBoxEnd.Text);
                cev.name = TextBoxName.Text;

                var dtime = cev.eventend.Value.TimeOfDay;
                int h = Convert.ToInt32(dtime.TotalHours);
                int th = 24;
                if (Convert.ToDateTime(TextBoxEnd.Text).Date > Convert.ToDateTime(TextBoxStart.Text).Date)
                {
                    lblMessage.Text = "You have entered a wrong date";
                    lblMessage.Visible = true;
                    // ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:alert(You have entered a wrong date);", true);
                }
                else if (h > th)
                {

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:alert(You have entered a wrong date);", true);
                }
                else
                {



                    if (Request.QueryString["Id"] != null)
                    {
                        Session["doctorId"] = Request.QueryString["Id"].ToString();
                        // FillOpenViewLabels();
                    }
                    cev.doctorID = Convert.ToInt32(Session["doctorId"]);

                    AddEventd(cev);
                    Modal.Close(this, "OK");
                }
            }
            {

                ScriptManager.RegisterStartupScript(this,typeof(Page), "MessagePopUp", "alert('Access restricted to Doctors and Dentists Please register or log in to enter.ID:  You can use your email address PW: You can use - temp1234 ');", true);
                //int DoctorId = Convert.ToInt32(Request.QueryString["Id"].ToString());
               // cm.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Access Denied ! You must be logged in as a Doctor or Dentist for Access.\\n\\nOn Return to the Home Page, please log in now with your email ID and your assigned Pass-Word (temp1234)'); window.location.href = '" + Session["strHttp"].ToString() + "doctor/doctorLogin.aspx" + "';", true);
                ClientScriptManager cm = Page.ClientScript;
               // ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:alert(Access Denied ! You must be logged in as a Doctor or Dentist for Access);", true);
               // Session["FacePageURL"] = Session["strHttp"].ToString() + "doctor/DentistLogin.aspx";
              //  Modal.Close(this, "OK");

                return;
               
            }
            
    }

    private string dbInsertEvent(DateTime start, DateTime end, string name, string resource)
    {
        initData();

        string id = Guid.NewGuid().ToString();

        #region Simulation of database update

        DataRow dr = table.NewRow();
        dr["start"] = start;
        dr["end"] = end;
        dr["id"] = id;
        dr["name"] = name;
        dr["column"] = resource;

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        return id;
    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
       Modal.Close(this);
    }

    private void initData()
    {
        if (Session["AllFeatures"] == null)
        {
           // Session["AllFeatures"] = DataGenerator.GetData();
        }
        table = (DataTable)Session["AllFeatures"];

    }
  


     public bool AddEventd(CalendarEvent modl)
     {
         DataCalendarDataContext DC = new DataCalendarDataContext();
         try
         {


             DC.CalendarEvents.InsertOnSubmit(modl);
            
             DC.SubmitChanges();
             return true;
         }
         catch
         {
             return false;
         }
     }
}
